<template>
  <div class="identify" @click="refreshCode">
    <canvas ref="identify" :width="contentWidth" :height="contentHeight"></canvas>
  </div>
</template>
<script>
import { mapMutations } from 'vuex'
export default {
  name: 'Identify',
  props: {
    identifyCode: {
      type: String,
      default: ''
    },
    fontSizeMin: {
      type: Number,
      default: 16
    },
    fontSizeMax: {
      type: Number,
      default: 32
    },
    backgroundColorMin: {
      type: Number,
      default: 180
    },
    backgroundColorMax: {
      type: Number,
      default: 240
    },
    colorMin: {
      type: Number,
      default:50
    },
    colorMax: {
      type: Number,
      default: 160
    },
    lineColorMin: {
      type: Number,
      default: 60
    },
    lineColorMax: {
      type: Number,
      default: 180
    },
    dotColorMin: {
      type: Number,
      default: 0
    },
    dotColorMax: {
      type: Number,
      default: 255
    },
    contentWidth: {
      type: Number,
      default: 100
    },
    contentHeight: {
      type: Number,
      default: 40
    }
  },
  methods: {
    ...mapMutations(['setIdent']),
    /**
     * 生成随机数
     */
    randomNum (min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    /**
     * 生成随机颜色
     */
    randomColor (min, max) {
      let r = this.randomNum(min, max)
      let g = this.randomNum(min, max)
      let b = this.randomNum(min, max)
      return `rgb(${r},${g},${b})`
    },
    /**
     * 生成四位随机数
     */
    productRandom () {
      let res = ''
      for (let i = 0; i < 4; i++) {
        res += (Math.random() * 8 + 1) | 0
      }
      console.log(res)
      this.setIdent(res)
      return res
    },
    /**
     * 绘制图像
     */
    drawPic () {
      let num = this.productRandom()
      let canvas = this.$refs.identify
      let ctx = canvas.getContext('2d')
      ctx.textBaseline = 'bottom'
      // 绘制背景
      ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
      // 绘制文字
      for (let i = 0; i < num.length; i++) {
        this.drawText(ctx, num[i], i)
      }
      this.drawLine(ctx)
      this.drawDot(ctx)
    },
    /**
     * 绘制文字
     */
    drawText(ctx, word, i) {
      ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
      ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
      let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
      let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
      let deg = this.randomNum(-45, 45)
      // 修改坐标原点和旋转角度
      ctx.translate(x, y)
      ctx.rotate((deg * Math.PI) / 180)
      ctx.fillText(word, 0, 0)
      ctx.rotate((-deg * Math.PI) / 180)
      ctx.translate(-x, -y)
    },
    /**
     * 绘制干扰线
     */
    drawLine (ctx) {
      for (let i = 0; i < 3; i++) {
        ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
        ctx.beginPath()
        ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
        ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
        ctx.stroke()
      }
    },
    /**
     * 绘制干扰点
     */
    drawDot (ctx) {
      for (let i = 0; i < 10; i++) {
        ctx.fillStyle = this.randomColor(0, 255)
        ctx.beginPath()
        ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
        ctx.fill()
      }
    },
    /**
     * 刷新验证码
     */
    refreshCode () {
      this.drawPic()
    }
  },
  // watch: {
  //   identifyCode () {
  //     this.drawPic()
  //   }
  // },
  mounted () {
    this.$nextTick(() => {
      console.log(1);
      this.refreshCode()
    })
  }
}
</script>
